package com.huatai.datacenter.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.common.util.StringUtil;
import com.huatai.datacenter.entity.RequestLogEntity;
import com.huatai.datacenter.entity.ServerObjectEntity;
import com.huatai.datacenter.entity.dto.RequestLogDto;
import com.huatai.datacenter.mapper.ApiServer;
import com.huatai.datacenter.service.HttpRequestService;
import com.huatai.datacenter.service.RequestService;
import com.huatai.datacenter.service.ServerObjectService;
import com.huatai.datacenter.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Lion
 * @date 2023/7/20  16:20
 */
@RequestMapping("/requestLog")
@RestController
@Api(value = "API服务访问日志", tags = "API服务访问日志")
public class RequestLogController extends BaseController<RequestService, RequestLogEntity>{

	@Autowired
	private HttpRequestService httpRequestService;

	@Autowired
	private ServerObjectService serverObjectService;

	@ApiOperation("首页：获取数据服务请求成功/失败的数量")
	@GetMapping("/getRequestStatusCount")
	public R queryRequestLog(){

		String result = httpRequestService.queryData();
		return R.data(JSONArray.parseArray(result));

	}

	@ApiServer(apiPath = "/data/testData")
	@RequestMapping("/testData")
	public String test(){

		List<RequestLogEntity> list = null;

		String key = RedisUtil.buildDbTableKey(RequestLogEntity.class);

		list = (List<RequestLogEntity>) RedisUtil.get(key,null);
		if (list == null){
			list = this.service.list().stream().sorted(Comparator.comparing(RequestLogEntity::getCreateTime).reversed()).collect(Collectors.toList());
			RedisUtil.set(key, list);
		}else {
			list = list.stream().sorted(Comparator.comparing(RequestLogEntity::getCreateTime).reversed()).collect(Collectors.toList());
		}

		Timestamp now =  Timestamp.valueOf(LocalDateTime.now());
		Timestamp data = Timestamp.valueOf(list.get(0).getCreateTime());

		long sta = now.getTime() - data.getTime();
		long resultData = sta / 3600000;

		return String.valueOf(resultData);
	}

	/**
	 * 获取接口请求日志清册
	 * @param requestLogDto
	 * @return
	 */
	@RequestMapping("/getRequestLogList")
	@ApiOperationSupport(includeParameters = {
		"requestLogDto.page",
		"requestLogDto.limit",
		"requestLogDto.appKey",
		"requestLogDto.requestName"
	})
	@ApiOperation("获取接口请求日志清册")
	public R<Page<RequestLogEntity>> getRequestLogList(@RequestBody RequestLogDto requestLogDto){
		Page<RequestLogEntity> page = new Page<>(requestLogDto.getPage(), requestLogDto.getLimit());
		LambdaQueryWrapper<RequestLogEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.apply(StringUtil.isNotBlank(requestLogDto.getAppKey()),"detail->'$.appKey' = '"+requestLogDto.getAppKey()+"'");
		queryWrapper.like(StringUtil.isNotBlank(requestLogDto.getRequestName()),RequestLogEntity::getRequestName,requestLogDto.getRequestName());
		this.service.page(page,queryWrapper);
		return R.data(page);
	}

	/**
	 * 获取服务对象下拉框清册
	 * @return
	 */
	@RequestMapping("getServerObject")
	@ApiOperation("获取服务对象下拉框清册")
	public R<List<JSONObject>> getServerObject(){
		return serverObjectService.getServerObject();
	}

}
